<template>
	<view>
		<u-swiper :list="swiperList" height="350" :autoplay="false" circular radius="0" indicatorStyle="right: 20px">
		</u-swiper>
		<view class="mainpadding" style="padding-bottom: 150rpx;">
			<view class="mainpadding ffffff">
				<view class="titletext bold hei_text">{{dataAll.name}}</view>
			</view>
			<view class="margin_top">
				<view class="ffffff radius" v-for="(item,index) in spuList" :key="item.id">
					<view class="flexbetween mainpadding2s" style="background-color: #f8b62a;">
						<view class="titletext hei_text bold">{{item.name}}</view>
						<template v-if="dataAll.is_staff==1">
							<view class="xiaohei main_size bold" v-if="item.ygnum_b!=0">{{item.ygfh==1?'':'至少'}}選擇{{item.ygnum_b}}個
							</view>
						</template>
						<template v-if="dataAll.is_staff!=1">
							<view class="xiaohei main_size bold" v-if="item.num_b!=0">{{item.fh==1?'':'至少'}}選擇{{item.num_b}}個
							</view>
						</template>
					</view>
					<view class="mainpadding">
						<view class="flexleft margin_top" :class="i==item.spu_item.length-1?'':'pdl'"
							v-for="(value,i) in item.spu_item" :key="value.id">
							<view class="listleftimg2 radius margin_right">
								<image class="listleftimg2 radius" :src="value.image_text" mode=""></image>
							</view>
							<view class="flexbetween" style="width: 100%;">
								<view style="width: 60%;">
									<view class="hei_text main_size bold yihang">{{value.name}}</view>
									<view class="huang_text main_size bold margin_top">
										<text class="main_size color_red margin_right1" v-if="dataAll.is_staff==1">${{value.yg_money}}</text>
										<text class="main_size color_red margin_right1" v-if="dataAll.is_staff!=1">${{value.money}}</text>
									</view>
								</view>
								<u-number-box button-size="36" color="#000000" :min="0" bgColor="#F7F7F7"
									iconStyle="color: #fff" v-model="value.num"></u-number-box>
							</view>
						</view>
					</view>
				</view>
			</view>
			
		</view>
		<view class="posibottom mainpadding">
			<view class="flexbetween">
				<view class="leftAllprice flexleft mainpadding ffffff">
					<view class="hei_text main_size">合計：</view>
					<view class="color_red main_size" v-text="'$'+money"></view>
				</view>
				<view class="settlement" @click="submit()">確定</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				swiperList:[],
				dataAll:{},
				spuList:[]
			}
		},
		onLoad(options) {
			this.listid = options.id
		},
		onShow() {
			this.getDetail()
		},
		computed:{
			money() { //总价
				let price = 0
				let Allarr = []
				this.spuList.forEach(item => {
					let arr = []
					item.spu_item.forEach(value => {
						if (value.num) {
							for(let i=1;i<=Number(value.num);i++){
								if(this.dataAll.is_staff==1){
									arr.push(Number(value.yg_money))
								}else{
									arr.push(Number(value.money))
								}
							}
						}
					})
					arr.sort((a, b) => b - a);
					if(Number(item.num_b) >= arr.length){
						arr = []
					}else{
						if(item.num_b!=0){
							arr.splice(arr.length - Number(item.num_b), Number(item.num_b));
						}
					}
					console.log(arr,"sdg");
					Allarr = Allarr.concat(arr)
				})
				Allarr.forEach(item=>{
					price = Number(item) + Number(price)
				})
				return price
			}
		},
		methods: {
			getDetail(){
				this.http.request("/api/Shopwelfarecl/shopwelfareDetail", 'POST', {
					id: this.listid,
					shop_id: uni.getStorageSync("shop_id")
				}).then(res => {
					if(res.data.is_tx==0){
						this.http.toast("很抱歉，您無法參與當前調查！")
						setTimeout(()=>{
							uni.navigateBack(1)
						},500)
						return false
					}
					this.swiperList = res.data.images_text
					if(res.data.video_url_text){
						this.swiperList.unshift(res.data.video_url_text)
					}
					this.dataAll = res.data
					res.data.spu_data_text.forEach(item=>{
						item.spu_item.forEach(ite=>{
							this.$set(ite,'num',0)
						})
					})
					this.spuList = res.data.spu_data_text
				})
			},
			submit() {
				let spu_data = []
				for (let i = 0; i < this.spuList.length; i++) {
					let item = this.spuList[i]
					let num = 0
					let spu_item = []
					item.spu_item.forEach(value => {
						if (value.num != 0) {
							num = num + Number(value.num)
						}
						let total_price = 0
						if(this.dataAll.is_staff==1){
							total_price = Number(value.yg_money) * value.num
						}else{
							total_price = Number(value.money) * value.num
						}
						spu_item.push({
							name:value.name,
							money:value.money,
							image:value.image,
							yg_money:value.yg_money,
							num:value.num,
							total_price
						})
					})
					if(this.dataAll.is_staff==1){//員工
						if (item.ygfh == 1) { //等於
							if (num != Number(item.ygnum_b)) {
								this.http.toast(item.name + "的規格選擇" + item.ygnum_b + "個")
								return false
							}
						}
						if (item.ygfh == 2) { //大於等於
							if (num < Number(item.ygnum_b)) {
								this.http.toast(item.name + "的規格至少選擇" + item.ygnum_b + "個")
								return false
							}
						}
					}
					if(this.dataAll.is_staff!=1){//商家
						if (item.fh == 1) { //等於
							if (num != Number(item.num_b)) {
								this.http.toast(item.name + "的規格選擇" + item.num_b + "個")
								return false
							}
						}
						if (item.fh == 2) { //大於等於
							if (num < Number(item.num_b)) {
								this.http.toast(item.name + "的規格至少選擇" + item.num_b + "個")
								return false
							}
						}
					}
					spu_data.push({
						name:item.name,
						num_b:item.num_b,
						fh:item.fh,
						spu_item,
						ygnum_b:item.ygnum_b,
						ygfh:item.ygfh,
					})
				}
				uni.showLoading({
					mask:true,
					title:"請稍後"
				})
				this.http.request("/api/Shopwelfarecl/addOrder", 'POST', {
					fenshop_id: uni.getStorageSync("shop_id"),
					shop_welfare_id:this.listid,
					pay_price:this.money,
					spu_data
				}).then(res => {
					uni.hideLoading()
					if(res.code==1){
						if(this.money==0){
							uni.navigateBack(1)
							return false
						}
						uni.redirectTo({
							url: "/pages/my/payment?paytype=3&paymoney=" + this.money+"&id="+res.data.order_id+"&shop_id="+this.dataAll.shop_id
						})
					}else{
						this.http.toast(res.msg)
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
::v-deep .u-number-box__minus {
		background-color: #EC7AA0 !important;
	}

	::v-deep .u-number-box__plus {
		background-color: #EC7AA0 !important;
	}

	::v-deep .uicon-minus {
		color: #ffffff !important;
	}

	::v-deep .uicon-plus {
		color: #ffffff !important;
	}
	.settlement {
		width: 145rpx;
		height: 101rpx;
		line-height: 101rpx;
		text-align: center;
		background: #ed4d2d;
		border-radius: 0rpx 53rpx 53rpx 0rpx;
		font-size: 32rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #fff;
	}
</style>
